package whu.o2o.connection;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;

import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;

import whu.o2o.util.Utils;

public class DownloadAudioTask implements Runnable {

	private InputStream in;
	String fileName;
	Params[] params;
	File file;
	Activity mcontext;
	Handler handler;
	String tag = "";
	String s;
	ProgressBar pb;
	ImageView sound;
	String time = "";
	int mposition;

	public DownloadAudioTask(Params[] params, String fileName) {
		this.params = params;
		this.fileName = fileName;
		this.handler = params[0].getHandler();
		s = fileName.substring(fileName.lastIndexOf("/") + 1);
		System.out.println("=======" + s);
		file = new File(Utils.AUDIO_PATH + s);
	}

	public DownloadAudioTask(Activity mcontext, Params[] params, String fileName, ImageView sound, ProgressBar pb,
			String time, int mposition) {
		this.mposition = mposition;
		this.time = time;
		this.mcontext = mcontext;
		this.params = params;
		this.fileName = fileName;
		this.pb = pb;
		this.sound = sound;
		this.handler = params[0].getHandler();
		file = new File(Utils.AUDIO_PATH + fileName);
	}

	public void run() {
		// TODO Auto-generated method stub
		Params p = params[0];
		HttpClient httpClient = null;
		if (p.getRequestType().equals("get")) {
			// �����Ӳ���������
			try {
				httpClient = new DefaultHttpClient();
				HttpGet request = new HttpGet(p.getUrl());
				HttpConnectionParams.setConnectionTimeout(request.getParams(), 10 * 000); // ��������ʱʱ��
				HttpConnectionParams.setSoTimeout(request.getParams(), 10 * 000);// ��ȡ��ʱ
				HttpResponse hr = httpClient.execute(request);
				HttpEntity entity = hr.getEntity();
				if (entity != null) {
					in = entity.getContent();
				}
				if (!file.exists()) {
					try {
						file.createNewFile();
					} catch (Exception e) {
						// TODO: handle exception
					}
				}
				FileOutputStream fos = new FileOutputStream(file);
				fos.write("".getBytes());
				byte[] b = new byte[1024];
				int a;
				while ((a = in.read(b, 0, 1024)) > 0) {
					fos.write(b, 0, a);
				}
				System.out.println("-------" + file.length());
				tag = s;
			} catch (Exception e) {
				tag = "";
				e.printStackTrace();
			} finally {
				if (sound != null || pb != null) {
					mcontext.runOnUiThread(new Runnable() {
						public void run() {
							// TODO Auto-generated method stub
							sound.setVisibility(View.VISIBLE);
							pb.setVisibility(View.GONE);
						}
					});
				}
				Message msg = handler.obtainMessage();
				msg.obj = tag;
				msg.arg1 = mposition;
				msg.sendToTarget();
				httpClient.getConnectionManager().shutdown();
			}
		}
	}

}
